package com.quan.upms.web.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.base.common.utils.Paginator;
import com.quan.upms.annotation.RequiresPermissions;
import com.quan.upms.rpc.api.UpmsRoleService;
import com.quan.upms.rpc.extend.UpmsRoleDto;
import com.quan.upms.rpc.model.UpmsRole;

@RestController
@RequestMapping(value = "/api/upms/role")
public class UpmsRoleController extends BaseController {

	@Autowired
	private UpmsRoleService upmsRoleService;

	@RequiresPermissions(value="upms:role:add", description="添加角色")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody UpmsRole entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		if (null != this.upmsRoleService.checkValue("name", entity.getName())) {
			return BaseResult.failure("名称已存在");
		}
		if (null != this.upmsRoleService.checkValue("code", entity.getCode())) {
			return BaseResult.failure("编码已存在");
		}
		this.upmsRoleService.insertSelective(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:role:delete", description="删除角色")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.upmsRoleService.delete(id);
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="upms:role:delete", description="删除角色")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.upmsRoleService.deleteByIds(ids) ;
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:role:update", description="修改角色")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody UpmsRole entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		UpmsRole oldRole = this.upmsRoleService.selectById(entity.getId()) ;
		if(null != oldRole.getName() && !oldRole.getName().equals(entity.getName())) {
			if(null != this.upmsRoleService.checkValue("name", entity.getName())) {
				return BaseResult.failure("名称已存在");
			}
		}
		if(null != oldRole.getCode() && !oldRole.getCode().equals(entity.getCode())) {
			if(null != this.upmsRoleService.checkValue("code", entity.getCode())) {
				return BaseResult.failure("编码已存在");
			}
		}
		this.upmsRoleService.updateSelective(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:role:detail", description="查询角色详情")
	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Integer id) {
		return this.upmsRoleService.selectById(id);
	}

	@RequiresPermissions(value="upms:role:list", description="查询角色")
	@GetMapping(value = "/list")
	public Object list(UpmsRole entity) {
		return this.upmsRoleService.selectPage(new Paginator(this.request), entity);
	}
	
	/**
	 * 将用户移除角色
	 * @param groupAssingId
	 * @return
	 */
	@RequiresPermissions(value="upms:role:remove_user_assign", description="移除该角色成员")
	@GetMapping(value = "/remove_role_users")
	public Object remove_role_users(String ids) {
		this.upmsRoleService.removeRoleUsers(ids) ;
		return BaseResult.ok();
	}
	
	/**
	 * 添加权限到角色
	 * @param dto
	 * @return
	 */
	@RequiresPermissions("upms:role:add_permission")
	@PostMapping(value = "/addPermissionToRole")
	public Object addPermissionToRole(@RequestBody UpmsRoleDto dto) {
		UpmsRole selectById = this.upmsRoleService.selectById(dto.getId()) ;
		if(null == selectById) {
			return BaseResult.failure("该角色不存在");
		}
		this.upmsRoleService.addPermissionToRole(dto);
		return BaseResult.ok();
	}
	
	/**
	 * 获取角色与权限的关联关系
	 * @param entity
	 * @return
	 */
	@GetMapping(value = "/getRolePermissionAssign")
	public Object getRolePermissionAssign(UpmsRole entity) {
		UpmsRole selectById = this.upmsRoleService.selectById(entity.getId()) ;
		if(null == selectById) {
			return BaseResult.failure("该角色不存在");
		}
		return this.upmsRoleService.getRolePermissionAssign(entity.getId()) ;
	}
}
